package org.dicker.controller;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dicker.biz.TypeBiz;
import org.dicker.biz.impl.TypeBizImpl;
import org.dicker.entity.MessageInfo;
import org.dicker.entity.TypeInfo;
import org.lanqiao.mvc.entity.ModelAndView;
import org.lanqiao.mvc.entity.MultipartFile;
import org.lanqiao.mvc.entity.YockMvcAnnotation.Controller;
import org.lanqiao.mvc.entity.YockMvcAnnotation.RequestMapping;
import org.lanqiao.mvc.entity.YockMvcAnnotation.RequestParam;
import org.lanqiao.mvc.entity.YockMvcAnnotation.ResponseBody;
import org.lanqiao.mvc.entity.YockMvcAnnotation.ResponseDispatch;
import org.lanqiao.mvc.entity.YockMvcAnnotation.UploadFile;

@Controller("/type")
public class TypeController {
	
	private TypeBiz tbiz = new TypeBizImpl();
	@RequestMapping(value = "/list.do") 
	@ResponseDispatch(value = "/WEB-INF/console/type/type-list.jsp")
	public ModelAndView index(
			HttpServletRequest req,
			HttpServletResponse res,
			@RequestParam(name = "typename") String typename,
			@RequestParam(name = "page") String page){
		ModelAndView mv = new ModelAndView();
		
		if(typename==null){
			typename="";
		}
		int pageSize=5;
		int newpage=0;
		if(page==null){
			page="1";
		}
		newpage=Integer.parseInt(page);
		List<TypeInfo> typeList = tbiz.selectByList(typename, newpage, pageSize);
		mv.addObject("typeList", typeList);
		
		try {
			int totalcount = tbiz.selectCount(typename);
			int totalpage=totalcount%pageSize==0 ? totalcount/pageSize : totalcount/pageSize+1;
			mv.addObject("totalpage", totalpage);
			mv.addObject("newpage", newpage);
			
			////////////////////
			//每个页码组显示5个
			int groupSize=5;
			
			//根据当前第几页求出位于第几组
			int currentGroup = (newpage-1)/groupSize + 1;
				
			//计算出当前这一组应该显示的页码范围
			//（startPage  ~  endPage）
			//（1  ~ 5）	   第1组
			//（6 ~ 10）	   第2组
			//（11 ~ 15）	   第3组
			//（16 ~ 20）	   第4组
			int startPage = (currentGroup-1)*groupSize+1;
			int endPage = currentGroup*groupSize;
			mv.addObject("startPage", startPage);
			mv.addObject("endPage", endPage);
			////////////////////
			
			
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		return mv;
	}
	
	
	
	@RequestMapping(value = "/type_delete.do") 
	@ResponseBody
	public String deleteById(
			HttpServletRequest req,
			HttpServletResponse res,
			@RequestParam(name = "typeid") int typeid)
					throws Exception{
		req.setCharacterEncoding("utf-8");
		res.setCharacterEncoding("utf-8");
		res.setContentType("text/html; charset=UTF-8");
		int num = tbiz.deleteById(typeid);
		String str="";
		if(num==1){
			str="<script>alert('删除成功');location.href='/big-birds/type/list.do'</script>";
		}else{
			str="<script>alert('删除失败!请重试');location.reload()</script>";
		}
		
		return str;
	}
	
	
	@RequestMapping(value = "/type_updatepage.do") 
	@ResponseDispatch(value = "/WEB-INF/console/type/type_update.jsp")
	public ModelAndView messageUpdatePage(
			HttpServletRequest req,
			HttpServletResponse res,
			@RequestParam(name = "id") int id)
					throws Exception{
		ModelAndView mv = new ModelAndView();
		
		TypeInfo tinfo = tbiz.selectByInfo(id);
		mv.addObject("tinfo", tinfo);
		
		return mv;
	}
	
	@RequestMapping(value = "/type_update.do")
	@UploadFile
	@ResponseBody
	public String messageUpdate(
			HttpServletRequest req,
			HttpServletResponse res,
			@RequestParam(name = "type_type") int type_type,
			@RequestParam(name = "type_name") String type_name,
			@RequestParam(name = "icon_url") MultipartFile icon_url,
			@RequestParam(name = "type_id") int type_id)
					throws Exception{
		
		req.setCharacterEncoding("utf-8");
		res.setCharacterEncoding("utf-8");
		res.setContentType("text/html; charset=UTF-8");
		
		System.out.println("type_type is "+type_type);
		System.out.println("type_name is "+type_name);
		System.out.println("icon_url is "+icon_url);
		System.out.println("type_id is "+type_id);
		
		//获取文件的完整名称
		//bookLogoSmall.getName()
		String houzhui = icon_url.getName().substring(icon_url.getName().lastIndexOf("."));	
		String imgName = System.currentTimeMillis()+"";
		//读取web项目中upload文件夹的所在tomcat的真实路径
		String prefix = req.getServletContext().getRealPath("upload");
			System.out.println(prefix);
			
		//////////////核心代码（写入文件）/////////////////////
		File file = new File(prefix+"/"+imgName+houzhui);
		icon_url.transferTo(file);
		String imgStr=imgName+houzhui;
		
		TypeInfo tinfo=new TypeInfo();
		tinfo.setType_type(type_type);
		tinfo.setType_name(type_name);
		tinfo.setIcon_url(imgStr);
		tinfo.setType_id(type_id);
		
		
		
		String str="";
		if(tbiz.updateById(tinfo)){
			str="<script>alert('修改成功');parent.location.href='/big-birds/type/list.do'</script>";
		}else{
			str="<script>alert('修改失败!请重试');parent.location.reload()</script>";
		}

		return str;
	}
	
	
	
	@RequestMapping(value = "/type_addpage.do") 
	@ResponseDispatch(value = "/WEB-INF/console/type/type_add.jsp")
	public ModelAndView messageAddPage(
			HttpServletRequest req,
			HttpServletResponse res
			)
					throws Exception{
		ModelAndView mv = new ModelAndView();
		
		return mv;
	}
	
	
	@RequestMapping(value = "/type_add.do")
	@UploadFile
	@ResponseBody
	public String messageAdd(
			HttpServletRequest req,
			HttpServletResponse res,
			@RequestParam(name = "type_type") int type_type,
			@RequestParam(name = "type_name") String type_name,
			@RequestParam(name = "icon_url") MultipartFile icon_url
						)throws Exception{
		
		req.setCharacterEncoding("utf-8");
		res.setCharacterEncoding("utf-8");
		res.setContentType("text/html; charset=UTF-8");
		
		System.out.println("type_type is "+type_type);
		System.out.println("type_name is "+type_name);
		System.out.println("icon_url is "+icon_url);
		String imgStr="";
		if(icon_url!=null){
			//获取文件的完整名称
			//bookLogoSmall.getName()
			String houzhui = icon_url.getName().substring(icon_url.getName().lastIndexOf("."));	
			String imgName = System.currentTimeMillis()+"";
			//读取web项目中upload文件夹的所在tomcat的真实路径
			String prefix = req.getServletContext().getRealPath("upload");
				System.out.println(prefix);
				
			//////////////核心代码（写入文件）/////////////////////
			File file = new File(prefix+"/"+imgName+houzhui);
			icon_url.transferTo(file);
			imgStr=imgName+houzhui;
		}else{
			imgStr=null;
		}
		
		TypeInfo tinfo=new TypeInfo();
		tinfo.setType_type(type_type);
		tinfo.setType_name(type_name);
		tinfo.setIcon_url(imgStr);
		
		String str="";
		if(tbiz.addType(tinfo)){
			str="<script>alert('添加成功');parent.location.href='/big-birds/type/list.do'</script>";
		}else{
			str="<script>alert('添加失败!请重试');parent.location.reload()</script>";
		}

		return str;
	}
	
}
